사이트 내 전체검색
PHP
[php] php 소스 압축 함수 php_strip_whitespace
로빈아빠
https://cmd.kr/php/541 URL이 복사되었습니다.

본문

php 소스 압축 함수 php_strip_whitespace

js나 css는 Minnify이나 jsmin과 같은 유틸을 이용하여 용량을 줄일 수 있습니다.
그럼 php는 php_strip_whitespace로 줄일 수 있습니다.

php_strip_whitespace (PHP 5) - Return source with stripped comments and whitespace

string php_strip_whitespace ( string $filename ) Returns the PHP source code in filename with PHP comments and whitespace removed. This may be useful for determining the amount of actual code in your scripts compared with the amount of comments. This is similar to using php -w from the commandline.
아주 간단하죠 변수에 파일 이름만 넣으면 됩니다.
예로서 아래 소스를
<?php
// PHP comment here

/*
 * Another PHP comment
 */

echo        php_strip_whitespace(__FILE__);
// Newlines are considered whitespace, and are removed too:
do_nothing();
?>

적용 시키면 아래와 같이 결과값이 나옵니다.
<?php
 echo php_strip_whitespace(__FILE__); do_nothing(); ?>


하지만 php_strip_whitespace 내장 함수는 PHP5에서만 동작 합니다.
그럼 PHP4에서는 아래 함수를 사용하시면 됩니다.
<?php
//php_strip_whitespace PHP5에 있는 함수 대신 PHP4에 적용
function compress_php_src($src) {
		// Whitespaces left and right from this signs can be ignored
		static $IW = array(
				T_CONCAT_EQUAL,             // .=
				T_DOUBLE_ARROW,             // =>
				T_BOOLEAN_AND,              // &&
				T_BOOLEAN_OR,               // ||
				T_IS_EQUAL,                 // ==
				T_IS_NOT_EQUAL,             // != or <>
				T_IS_SMALLER_OR_EQUAL,      // <=
				T_IS_GREATER_OR_EQUAL,      // >=
				T_INC,                      // ++
				T_DEC,                      // --
				T_PLUS_EQUAL,               // +=
				T_MINUS_EQUAL,              // -=
				T_MUL_EQUAL,                // *=
				T_DIV_EQUAL,                // /=
				T_IS_IDENTICAL,             // ===
				T_IS_NOT_IDENTICAL,         // !==
				T_DOUBLE_COLON,             // ::
				T_PAAMAYIM_NEKUDOTAYIM,     // ::
				T_OBJECT_OPERATOR,          // ->
				T_DOLLAR_OPEN_CURLY_BRACES, // ${
				T_AND_EQUAL,                // &=
				T_MOD_EQUAL,                // %=
				T_XOR_EQUAL,                // ^=
				T_OR_EQUAL,                 // |=
				T_SL,                       // <<
				T_SR,                       // >>
				T_SL_EQUAL,                 // <<=
				T_SR_EQUAL,                 // >>=
		);
		if(is_file($src)) {
				if(!$src = file_get_contents($src)) {
						return false;
				}
		}
		$tokens = token_get_all($src);

		$new = "";
		$c = sizeof($tokens);
		$iw = false; // ignore whitespace
		$ih = false; // in HEREDOC
		$ls = "";    // last sign
		$ot = null;  // open tag
		for($i = 0; $i < $c; $i++) {
				$token = $tokens[$i];
				if(is_array($token)) {
						list($tn, $ts) = $token; // tokens: number, string, line
						$tname = token_name($tn);
						if($tn == T_INLINE_HTML) {
								$new .= $ts;
								$iw = false;
						} else {
								if($tn == T_OPEN_TAG) {
										if(strpos($ts, " ") || strpos($ts, "\n") || strpos($ts, "\t") || strpos($ts, "\r")) {
												$ts = rtrim($ts);
										}
										$ts .= " ";
										$new .= $ts;
										$ot = T_OPEN_TAG;
										$iw = true;
								} elseif($tn == T_OPEN_TAG_WITH_ECHO) {
										$new .= $ts;
										$ot = T_OPEN_TAG_WITH_ECHO;
										$iw = true;
								} elseif($tn == T_CLOSE_TAG) {
										if($ot == T_OPEN_TAG_WITH_ECHO) {
												$new = rtrim($new, "; ");
										} else {
												$ts = " ".$ts;
										}
										$new .= $ts;
										$ot = null;
										$iw = false;
								} elseif(in_array($tn, $IW)) {
										$new .= $ts;
										$iw = true;
								} elseif($tn == T_CONSTANT_ENCAPSED_STRING || $tn == T_ENCAPSED_AND_WHITESPACE) {
										if($ts[0] == '"') {
												$ts = addcslashes($ts, "\n\t\r");
										}
										$new .= $ts;
										$iw = true;
								} elseif($tn == T_WHITESPACE) {
										$nt = @$tokens[$i+1];
												if(!$iw && (!is_string($nt) || $nt == '$') && !in_array($nt[0], $IW)) {
												$new .= " ";
										}
										$iw = false;
								} elseif($tn == T_START_HEREDOC) {
										$new .= "<<<S\n";
										$iw = false;
										$ih = true; // in HEREDOC
								} elseif($tn == T_END_HEREDOC) {
										$new .= "S;";
										$iw = true;
										$ih = false; // in HEREDOC
										for($j = $i+1; $j < $c; $j++) {
												if(is_string($tokens[$j]) && $tokens[$j] == ";") {
														$i = $j;
														break;
												} else if($tokens[$j][0] == T_CLOSE_TAG) {
														break;
												}
										}
								} elseif($tn == T_COMMENT || $tn == T_DOC_COMMENT) {
										$iw = true;
								} else {
										if(!$ih) {
												$ts = strtolower($ts);
										}
										$new .= $ts;
										$iw = false;
								}
						}
						$ls = "";
				} else {
						if(($token != ";" && $token != ":") || $ls != $token) {
								$new .= $token;
								$ls = $token;
						}
						$iw = true;
				}
		}
		return $new;
}
?>


For example:
<?php $src = <<<EOT <?php // some comment for ( $i = 0; $i < 99; $i ++ ) { echo "i=${ i }\n"; /* ... */ } /** ... */ function abc() { return "abc"; }; abc(); ?> <h1><?= "Some text " . str_repeat("_-x-_ ", 32);;; ?></h1> EOT; var_dump(compress_php_src($src)); ?>

And the result is:
string(125) "<?php for(=0;<99;++){echo "i=\n";}function abc(){return "abc";};abc(); ?> <h1><?="Some text ".str_repeat("_-x-_ ",32)?></h1>"

$src에 파일 이름을 넣어도 됩니다. 파일을 검사해 있으면 file_get_contents를 이용하여 파일을 읽어 옮니다.

댓글목록

등록된 댓글이 없습니다.

PHP
871 (9/18P)

Search

Copyright © Cmd 명령어 18.225.7.243